package com.qf.shiro2202day02.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qf.shiro2202day02.user.dao.*;
import com.qf.shiro2202day02.user.entity.*;
import com.qf.shiro2202day02.user.service.UserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qf.shiro2202day02.utils.SaltUtils;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author dong_
 * @since 2022-10-25
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserDao, User> implements UserService {

    @Autowired
    TUserRoleMapper tUserRoleMapper;

    @Autowired
    TRoleMapper tRoleMapper;

    @Autowired
    TRolePermsMapper tRolePermsMapper;

    @Autowired
    TPersMapper tPersMapper;

    @Autowired
    UserDao userDao;


    @Transactional(rollbackFor = Exception.class)
    @Override
    public void regist(User user) {

        //1. 密码生成

        final String salt = SaltUtils.getSalt(8);

        final Md5Hash md5Hash = new Md5Hash(user.getPassword(), salt, 1024);

        final String shiroPassword = md5Hash.toHex();

        user.setSalt(salt);

        user.setPassword(shiroPassword);

        this.save(user);


    }

    @Override
    @Transactional(propagation = Propagation.SUPPORTS)
    public List<String> getUserRole(User user) {

        QueryWrapper<TUserRole> wrapper = new QueryWrapper<>();

        wrapper.eq("userid",user.getId());

        List<TUserRole> tUserRoles = tUserRoleMapper.selectList(wrapper);



        //查询

        List<TRole> tRoles = tRoleMapper.selectList(null);

        //List<TRole>  ------>  Map<Integer, String>
        HashMap<Integer, String> rolemap = new HashMap<>();

        for (TRole tRole : tRoles) {
            rolemap.put(tRole.getId(),tRole.getName());
        }

        //List<TUserRole>  ------> List<String>

        List<String> list = tUserRoles.stream().
                map(tUserRole ->  rolemap.get(tUserRole.getRoleid()))
                .collect(Collectors.toList());

        return list;
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public List<String> getPermission(User user) {

        //1.根据

        QueryWrapper<TUserRole> wrapper = new QueryWrapper<>();

        wrapper.select("roleid");

        wrapper.eq("userid",user.getId());

        List<Object> roleIdList = tUserRoleMapper.selectObjs(wrapper);

        //2.
        QueryWrapper<TRolePerms> wrapper1 = new QueryWrapper<>();

        wrapper1.in("roleid",roleIdList);

        wrapper1.select("permsid");

        List<Object> permsIdList = tRolePermsMapper.selectObjs(wrapper1);

        HashSet<Object> hashSet = new HashSet<>();

        hashSet.addAll(permsIdList);

        //3.
        List<TPers> persList = tPersMapper.selectList(null);

        HashMap<Integer, String> hashMap = new HashMap<>();


        for (TPers pers : persList) {
            hashMap.put(pers.getId(),pers.getUrl());
        }

        List<String> permsUrlList = hashSet.stream().
                map(o -> hashMap.get((Integer) o))
                .collect(Collectors.toList());

        return permsUrlList;
    }
    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public List<String> getPermissionBySql(User user) {
       List<String> permissionList = userDao.getPermissionBySql(user.getId());
        return permissionList;
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public List<String> getUserRoleBySql(User user) {

        List<String> rolesList = userDao.getUserRoleBySql(user.getId());

        return rolesList;
    }


}
